using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.StsAdmin;
using System.Collections.Specialized;
using Microsoft.SharePoint;

namespace DD.SharePoint.STSADM.Extensions
{
    public class SPGroupManagementCommand : ISPStsadmCommand
    {
        #region ISPStsadmCommand Members

        public string GetHelpMessage(string command)
        {
            return "-url <Full url to a site in SharePoint>\n" +
                "        -group <Group Name>\n" +
                "        [-owner <Group Name>]\n";
        }

        public int Run(string command, StringDictionary keyValues, out string output)
        {
            command = command.ToLowerInvariant();

            switch (command)
            { 
                case "setgroupproperties":
                    return SetGroupProperties(keyValues, out output);
                default:
                    throw new InvalidOperationException();
            }
        }
        #endregion     

        private int SetGroupProperties(StringDictionary keyValues, out string output)
        {
            if (!keyValues.ContainsKey("url"))
            {
                throw new InvalidOperationException("The url parameter was not specified");
            }

            if (!keyValues.ContainsKey("group"))
            {
                throw new InvalidOperationException("The group parameter was not specified");                
            }


            StringBuilder sb = new StringBuilder();

            String url = keyValues["url"];
            string group = keyValues["group"];
           
            SPWeb web = null;
            
            try
            { 
                web = new SPSite(url).OpenWeb();            
            }
            catch (Exception)
            {
                throw new InvalidOperationException("Error retrieving url '" + url + "'.");
            }

            try
            {
                string oldOwner = web.Groups[group].Owner.ToString();
                sb.Append("Owner was '" + web.Groups[group].Owner.ToString() + "'\n");

                if (keyValues["owner"] != null)
                    web.Groups[group].Owner = web.SiteGroups[keyValues["owner"]];

                web.Groups[group].Update();
                web.Update();

                sb.Append("Owner is '" + web.Groups[group].Owner.ToString() + "'\n\n");
                
                if (oldOwner != web.Groups[group].Owner.ToString())                
                    sb.Append("Operation successfully completed");
                else
                    throw new InvalidOperationException("Owner has not been modified");
            }
            catch (Exception)
            {
                throw new InvalidOperationException("Group '" + group + "' does not exist at this site level.");
            }

            output = sb.ToString();

            return 0;
        }
    }
}
